package pl.edu.mimuw.irs.rcp.controller.editor.generator.part;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gef.EditPolicy;
import org.eclipse.swt.graphics.Font;

import pl.edu.mimuw.irs.core.model.petrinet.Place;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.part.PlaceEditPart;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.policy.NodeComponentEditPolicy;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.policy.WorkflowXYLayoutEditPolicy;
import pl.edu.mimuw.irs.rcp.util.enums.EnumFont;
import pl.edu.mimuw.irs.rcp.view.figure.FigureWrapper;
import pl.edu.mimuw.irs.rcp.view.figure.SimulationPlaceShape;



/**
 * @author Jakub Rauch
 * Created on: 2009-05-28
 */
public class GeneratorPlaceEditPart extends PlaceEditPart implements GeneratorEditPart, PropertyChangeListener {

	// *** Attributes

	// *** Methods
	
	public void activate() {
		getNode().addPropertyChangeListener(this);
		super.activate();
	};

	public void deactivate() {
		getNode().removePropertyChangeListener(this);
		super.deactivate();
	};
	
	protected void refreshVisuals() {
		
		FigureWrapper wrapper = (FigureWrapper)getFigure();
		SimulationPlaceShape placeShape = (SimulationPlaceShape)wrapper.getFigure();
		Place node = (Place)getModel();
		placeShape.setTokens(node.getTokens());
		
		Label label;
		wrapper.setLineWidth(1);
		label = new Label(node.getName());
		Font placeFont = EnumFont.ARIAL_8.getInstance();

		label.setFont(placeFont);
		label.setTextAlignment(PositionConstants.CENTER);
        label.setTextPlacement(PositionConstants.SOUTH);
		label.setBounds(wrapper.getBounds().crop(IFigure.NO_INSETS));
		wrapper.setLabel(label);
		
		placeShape.setTokens(node.getTokens());
		
		super.refreshVisuals();
	}
	
	protected NodeShape getNodeShape() {
		return NodeShape.TGPLACE;
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		//refresh(getRoot());
	}
//	
//	@SuppressWarnings("unchecked")
//	private void refresh(EditPart editPart) {
//		editPart.refresh();
//		List<EditPart> children = editPart.getChildren();
//		for (EditPart childEditPart : children) {
//			refresh(childEditPart);
//		}
//	}

	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new WorkflowXYLayoutEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new NodeComponentEditPolicy());
	}
}
